//
//  GSImportantNewsViewController.m
//  GameSky
//
//  Created by lanou on 16/1/26.
//  Copyright © 2016年 NiRongfei. All rights reserved.
//

#import "GSImportantNewsViewController.h"
#import "GSSubscrebe_Add_Topic_Details.h"
#import "GSSubscribeViewController_Add_Topic_Details_Cell.h"
#import "GSImportantNewsViewController_Next.h"
@interface GSImportantNewsViewController () <UITableViewDataSource,UITableViewDelegate>
/**
 * 展示页面
 */
@property (nonatomic, strong) UITableView *tableView;
// 跳转界面
@property (nonatomic, strong) GSImportantNewsViewController_Next *nextVC;
/**
 * 数据请求
 */
// 请求参数里  起始index
@property (nonatomic, assign) NSInteger startIndex;
// 数据源数组
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation GSImportantNewsViewController
#pragma mark-| 视图加载完成
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
// 数据请求  起始index 初始化
    _startIndex = 1;
// 数据请求
    [self loadData];
// 设置导航bar
    [self setUpNavigationbar];
// 添加tableView
    [self addTableView];
}

#pragma mark-| 设置导航bar
- (void)setUpNavigationbar{
self.bar.titleLabel.text = @"要闻";
}

#pragma mark-| 数据源 数组懒加载
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

#pragma mark-| 解析数据
- (void)loadData{
// 构建 参数字典
    NSDictionary *dic = @{@"request":@{@"parentNodeId":@"yaowen",
                                       @"nodeIds":@"0",
                                       @"pageIndex":[NSString stringWithFormat:@"%ld",_startIndex],
                                       @"elementsCountPerPage":@"20"
                                      }
                          };
// 网路请求
    [GSNetworkManager requestWithUrlStr:Subscribe_Add_Topic_Details paraDict:dic method:POST success:^(NSURLResponse *response, NSData *data) {
        if (data) {
            NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableLeaves) error:nil];
            if (_startIndex == 1) {
                [self.dataArray removeAllObjects];
            }
            // 获取 所需数组
            NSArray *array = dic[@"result"];
            for (NSDictionary *dict in array) {
                GSSubscrebe_Add_Topic_Details *model = [[GSSubscrebe_Add_Topic_Details alloc] init];
                [model setValuesForKeysWithDictionary:dict];
                [self.dataArray addObject:model];
            }
            
            
            // 刷新
            [_tableView.mj_header endRefreshing];
            [_tableView.mj_footer endRefreshing];
            [_tableView reloadData];
        }
    } failure:^(NSError *error) {
        
    }];

}

#pragma mark-| 添加tableView
- (void)addTableView{
// 初始化
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64) style:(UITableViewStylePlain)];
// 代理
    _tableView.delegate = self;
    _tableView.dataSource = self;
    // 设置cell的高度
    _tableView.rowHeight = kScreenWidth * 0.225;
// 添加刷新
    // 下拉
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _startIndex = 1;
        [self loadData];
    }];
    // 上提
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _startIndex += 1;
        [self loadData];
    }];
    
    
    [self.view addSubview:_tableView];
}


#pragma mark-| tableView协议
// 每个分区多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
// 创建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";
    GSSubscribeViewController_Add_Topic_Details_Cell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[GSSubscribeViewController_Add_Topic_Details_Cell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
    }
    GSSubscrebe_Add_Topic_Details *model = _dataArray[indexPath.row];
    cell.model = model;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// 获取 model
    GSSubscrebe_Add_Topic_Details *model = _dataArray[indexPath.row];
    _nextVC = [[GSImportantNewsViewController_Next alloc] init];
    _nextVC.contentID = model.contentId;
    _nextVC.view.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight);
    [[UIApplication sharedApplication].keyWindow addSubview:_nextVC.view];
[UIView animateWithDuration:0.5 animations:^{
    _nextVC.view.transform = CGAffineTransformTranslate(_nextVC.view.transform, -kScreenWidth, 0);
}];

}
#pragma mark-----------------------------------------
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
